package aliyun.cx.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import aliyun.cx.admin.entity.Article;
import aliyun.cx.admin.mapper.ArticleMapper;
import aliyun.cx.admin.service.ArticleService;
import aliyun.cx.admin.service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    
    @Autowired
    private ArticleTagService articleTagService;
    
    @Override
    public Page<Article> getArticlePage(Integer current, Integer size) {
        Page<Article> page = new Page<>(current, size);
        Page<Article> result = baseMapper.selectArticlePage(page);
        
        // 为每篇文章获取标签信息
        if (result.getRecords() != null) {
            for (Article article : result.getRecords()) {
                article.setTags(articleTagService.getTagsByArticleId(article.getId()));
            }
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean createArticle(Article article) {
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setViewCount(0);
        article.setLikeCount(0);
        if (article.getIsPublished() == null) {
            article.setIsPublished(0); // 默认草稿状态
        }
        
        // 保存文章
        boolean success = save(article);
        
        // 处理标签关联
        if (success && article.getTagIds() != null && !article.getTagIds().isEmpty()) {
            articleTagService.setArticleTags(article.getId(), article.getTagIds());
        }
        
        return success;
    }
    
    @Override
    @Transactional
    public boolean updateArticle(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        // 清除非数据库字段，避免更新时出错
        article.setAuthorName(null);
        article.setCategoryName(null);
        article.setTags(null);
        
        // 保存标签ID列表，然后清除
        List<Long> tagIds = article.getTagIds();
        article.setTagIds(null);
        
        // 更新文章
        boolean success = updateById(article);
        
        // 处理标签关联
        if (success) {
            articleTagService.setArticleTags(article.getId(), tagIds);
        }
        
        return success;
    }
    
    @Override
    @Transactional
    public boolean deleteArticle(Long id) {
        // 先删除文章标签关联
        articleTagService.deleteByArticleId(id);
        // 删除文章
        return removeById(id);
    }
    
    @Override
    public boolean togglePublishStatus(Long id, boolean isPublished) {
        // 使用基本查询方法获取文章信息，避免包含非数据库字段
        Article article = getById(id);
        if (article != null) {
            article.setIsPublished(isPublished ? 1 : 0);
            article.setUpdateTime(LocalDateTime.now());
            return updateById(article);
        }
        return false;
    }
    
    @Override
    public Article getArticleWithTags(Long id) {
        // 获取文章基本信息
        Article article = baseMapper.selectArticleById(id);
        if (article != null) {
            // 获取文章标签
            article.setTags(articleTagService.getTagsByArticleId(id));
        }
        return article;
    }
}
